<?php

/**
 * rt_approve_comment actions.
 *
 * @package    sf_sandbox
 * @subpackage rt_approve_comment
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class rt_approve_commentActions extends sfActions
{
  public function executeIndex()
  {
    return $this->forward('rt_approve_comment', 'list');
  }

  public function executeList()
  {
    $this->processSort();

    $this->processFilters();

    $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/rt_approve_comment/filters');

    // pager
    $this->pager = new sfPropelPager('RtApproveComment', 20);
    $c = new Criteria();
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/rt_approve_comment')));
    $this->pager->init();
    // save page
    if ($this->getRequestParameter('page')) {
        $this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/rt_approve_comment');
    }
  }

  public function executeCreate()
  {
    return $this->forward('rt_approve_comment', 'edit');
  }

  public function executeSave()
  {
    return $this->forward('rt_approve_comment', 'edit');
  }

  public function executeEdit()
  {
    $this->rt_approve_comment = $this->getRtApproveCommentOrCreate();

    if ($this->getRequest()->getMethod() == sfRequest::POST)
    {
      $this->updateRtApproveCommentFromRequest();

      $this->saveRtApproveComment($this->rt_approve_comment);

      $this->setFlash('notice', 'Thông tin đã được cập nhật');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('rt_approve_comment/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('rt_approve_comment/list');
      }
      else
      {
        return $this->redirect('rt_approve_comment/edit?id='.$this->rt_approve_comment->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }

  public function executeDelete()
  {
    $this->rt_approve_comment = RtApproveCommentPeer::retrieveByPk($this->getRequestParameter('id'));
    $this->forward404Unless($this->rt_approve_comment);

    try
    {
      $this->deleteRtApproveComment($this->rt_approve_comment);
    }
    catch (PropelException $e)
    {
      $this->getRequest()->setError('delete', 'Could not delete the selected Rt approve comment. Make sure it does not have any associated items.');
      return $this->forward('rt_approve_comment', 'list');
    }

    return $this->redirect('rt_approve_comment/list');
  }

  public function handleErrorEdit()
  {
    $this->preExecute();
    $this->rt_approve_comment = $this->getRtApproveCommentOrCreate();
    $this->updateRtApproveCommentFromRequest();

    $this->labels = $this->getLabels();

    return sfView::SUCCESS;
  }

  protected function saveRtApproveComment($rt_approve_comment)
  {
    $rt_approve_comment->save();

  }

  protected function deleteRtApproveComment($rt_approve_comment)
  {
    $rt_approve_comment->delete();
  }

  protected function updateRtApproveCommentFromRequest()
  {
    $rt_approve_comment = $this->getRequestParameter('rt_approve_comment');

    if (isset($rt_approve_comment['title']))
    {
      $this->rt_approve_comment->setTitle($rt_approve_comment['title']);
    }
    if (isset($rt_approve_comment['description']))
    {
      $this->rt_approve_comment->setDescription($rt_approve_comment['description']);
    }
  }

  protected function getRtApproveCommentOrCreate($id = 'id')
  {
    if (!$this->getRequestParameter($id))
    {
      $rt_approve_comment = new RtApproveComment();
    }
    else
    {
      $rt_approve_comment = RtApproveCommentPeer::retrieveByPk($this->getRequestParameter($id));

      $this->forward404Unless($rt_approve_comment);
    }

    return $rt_approve_comment;
  }

  protected function processFilters()
  {
    if ($this->getRequest()->hasParameter('filter'))
    {
      $filters = $this->getRequestParameter('filters');

      $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/rt_approve_comment');
      $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/rt_approve_comment/filters');
      $this->getUser()->getAttributeHolder()->add($filters, 'sf_admin/rt_approve_comment/filters');
    }
  }

  protected function processSort()
  {
    if ($this->getRequestParameter('sort'))
    {
      $this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'sf_admin/rt_approve_comment/sort');
      $this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'sf_admin/rt_approve_comment/sort');
    }

    if (!$this->getUser()->getAttribute('sort', null, 'sf_admin/rt_approve_comment/sort'))
    {
    }
  }

  protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['id_is_empty']))
    {
      $criterion = $c->getNewCriterion(RtApproveCommentPeer::ID, '');
      $criterion->addOr($c->getNewCriterion(RtApproveCommentPeer::ID, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['id']) && $this->filters['id'] !== '')
    {
      $c->add(RtApproveCommentPeer::ID, $this->filters['id']);
    }
    if (isset($this->filters['title_is_empty']))
    {
      $criterion = $c->getNewCriterion(RtApproveCommentPeer::TITLE, '');
      $criterion->addOr($c->getNewCriterion(RtApproveCommentPeer::TITLE, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['title']) && $this->filters['title'] !== '')
    {
      $c->add(RtApproveCommentPeer::TITLE, strtr($this->filters['title'], '*', '%'), Criteria::LIKE);
    }
  }

  protected function addSortCriteria($c)
  {
    if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/rt_approve_comment/sort'))
    {
      $sort_column = RtApproveCommentPeer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
      if ($this->getUser()->getAttribute('type', null, 'sf_admin/rt_approve_comment/sort') == 'asc')
      {
        $c->addAscendingOrderByColumn($sort_column);
      }
      else
      {
        $c->addDescendingOrderByColumn($sort_column);
      }
    }
  }

  protected function getLabels()
  {
    return array(
      'rt_approve_comment{id}' => 'ID:',
      'rt_approve_comment{title}' => 'Tiêu đề:',
      'rt_approve_comment{description}' => 'Nội dung:',
    );
  }	
}
